package 异常0907;

public class Student {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }
     //设置年龄
    public void setAge(int age) throws AgeException {//没有加try catch 则在方法后面继续抛出异常throws AgeException
         if (age<0) throw new AgeException();
        this.age = age;
    }
    public static void main(String[]args){
        Student s=new Student();
        try {
            s.setAge(-1);
            //处理异常
        } catch (AgeException e) {
            e.printStackTrace();
            System.out.println("年龄不能为负数！");
        }
    }
}
